package fr.lip6.move.care;

import fr.lip6.move.care.CareGen;
import fr.lip6.move.care.CompileResult;
import fr.lip6.move.care.SDMakerToCareUtils;
import java.util.ArrayList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import sdmakerz_v1.coresyntax.generator.SDMakerV1IDProvider;
import sdmakerz_v1.coresyntax.generator.interfaces.IContext;
import sdmakerz_v1.coresyntax.generator.interfaces.IDiagramRefData;
import sdmakerz_v1.coresyntax.generator.interfaces.IInterfaceData;
import sdmakerz_v1.coresyntax.sDMakerV1.Call;
import sdmakerz_v1.coresyntax.sDMakerV1.CombinableDiagramElement;

@SuppressWarnings("all")
public class CareCallGen {
  private CareGen mainGenerator;
  
  public CareCallGen(final CareGen mainGenerator) {
    this.mainGenerator = mainGenerator;
  }
  
  public CompileResult compile(final IContext context, final Call call) {
    ArrayList<CompileResult> _arrayList = new ArrayList<CompileResult>();
    ArrayList<CompileResult> nestedResults = _arrayList;
    CompileResult compRes = null;
    String _name = call.getName();
    String _plus = (_name + "_id");
    int _computeID = SDMakerV1IDProvider.computeID();
    String callID = (_plus + Integer.valueOf(_computeID));
    IDiagramRefData _diagramRefData = context.getDiagramRefData();
    String _caller = call.getCaller();
    String callerName = _diagramRefData.getNewName(_caller);
    IDiagramRefData _diagramRefData_1 = context.getDiagramRefData();
    String _called = call.getCalled();
    String calledName = _diagramRefData_1.getNewName(_called);
    StringConcatenation _builder = new StringConcatenation();
    _builder.newLine();
    {
      EList<CombinableDiagramElement> _nested_calls = call.getNested_calls();
      for(final CombinableDiagramElement elt : _nested_calls) {
        _builder.append("    ");
        CompileResult _compile = this.mainGenerator.compile(context, elt);
        CompileResult _compRes = compRes = _compile;
        CharSequence _nullExp = SDMakerToCareUtils.toNullExp(_compRes);
        _builder.append(_nullExp, "    ");
        _builder.newLineIfNotEmpty();
        _builder.append("    ");
        boolean _add = nestedResults.add(compRes);
        CharSequence _nullExp_1 = SDMakerToCareUtils.toNullExp(Boolean.valueOf(_add));
        _builder.append(_nullExp_1, "    ");
        _builder.append(" ");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    {
      int _size = nestedResults.size();
      boolean _equals = (_size == 0);
      if (_equals) {
        _builder.append("    ");
        _builder.append("Call ");
        _builder.append(callID, "    ");
        _builder.append(" = new Call(new Statement(\"S\" + i++,");
        _builder.append(callerName, "    ");
        _builder.append(",new Method(\"");
        String _name_1 = call.getName();
        _builder.append(_name_1, "    ");
        _builder.append("\",new ArrayList<ObjectInstance>(),");
        IInterfaceData _interfaceDataByObjectName = context.getInterfaceDataByObjectName(calledName);
        String _interfaceID = _interfaceDataByObjectName.getInterfaceID();
        _builder.append(_interfaceID, "    ");
        _builder.append("),");
        _builder.append(calledName, "    ");
        _builder.append("));        ");
        _builder.newLineIfNotEmpty();
      } else {
        int _size_1 = nestedResults.size();
        boolean _equals_1 = (_size_1 == 1);
        if (_equals_1) {
          _builder.append("    ");
          _builder.append("/* NESTED CALL: ");
          _builder.append(callID, "    ");
          _builder.append(" */");
          _builder.newLineIfNotEmpty();
          _builder.append("    ");
          CompileResult _get = nestedResults.get(0);
          CharSequence _charSequenceResult = _get.getCharSequenceResult();
          _builder.append(_charSequenceResult, "    ");
          _builder.newLineIfNotEmpty();
          _builder.append("    ");
          _builder.append("/* FINALIZING ");
          _builder.append(callID, "    ");
          _builder.append("*/");
          _builder.newLineIfNotEmpty();
          _builder.append("    ");
          _builder.append("Call ");
          _builder.append(callID, "    ");
          _builder.append(" = new Call(new Statement(\"S\" + i++,");
          _builder.append(callerName, "    ");
          _builder.append(",new Method(\"");
          String _name_2 = call.getName();
          _builder.append(_name_2, "    ");
          _builder.append("\",new ArrayList<ObjectInstance>(),");
          IInterfaceData _interfaceDataByObjectName_1 = context.getInterfaceDataByObjectName(calledName);
          String _interfaceID_1 = _interfaceDataByObjectName_1.getInterfaceID();
          _builder.append(_interfaceID_1, "    ");
          _builder.append("),");
          _builder.append(calledName, "    ");
          _builder.append("), ");
          CompileResult _get_1 = nestedResults.get(0);
          String _eltID = _get_1.getEltID();
          _builder.append(_eltID, "    ");
          _builder.append(");");
          _builder.newLineIfNotEmpty();
        } else {
          _builder.append("    ");
          _builder.append("/* NESTED CALL: ");
          _builder.append(callID, "    ");
          _builder.append(" */");
          _builder.newLineIfNotEmpty();
          _builder.append("    ");
          _builder.append("BlockList bloc_");
          _builder.append(callID, "    ");
          _builder.append("= new BlockList();");
          _builder.newLineIfNotEmpty();
          {
            for(final CompileResult nestedResult : nestedResults) {
              _builder.append("    ");
              CharSequence _charSequenceResult_1 = nestedResult.getCharSequenceResult();
              _builder.append(_charSequenceResult_1, "    ");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.newLine();
          _builder.append("    ");
          _builder.append("/* add(s) for ");
          _builder.append(callID, "    ");
          _builder.append(" */");
          _builder.newLineIfNotEmpty();
          {
            for(final CompileResult nestedResult_1 : nestedResults) {
              _builder.append("    ");
              _builder.append("bloc_");
              _builder.append(callID, "    ");
              _builder.append(".add(");
              String _eltID_1 = nestedResult_1.getEltID();
              _builder.append(_eltID_1, "    ");
              _builder.append(");");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.newLine();
          _builder.append("    ");
          _builder.append("/* FINALIZING ");
          _builder.append(callID, "    ");
          _builder.append("*/");
          _builder.newLineIfNotEmpty();
          _builder.append("    ");
          _builder.append("Call ");
          _builder.append(callID, "    ");
          _builder.append(" = new Call(new Statement(\"S\" + i++,");
          _builder.append(callerName, "    ");
          _builder.append(",new Method(\"");
          String _name_3 = call.getName();
          _builder.append(_name_3, "    ");
          _builder.append("\",new ArrayList<ObjectInstance>(),");
          IInterfaceData _interfaceDataByObjectName_2 = context.getInterfaceDataByObjectName(calledName);
          String _interfaceID_2 = _interfaceDataByObjectName_2.getInterfaceID();
          _builder.append(_interfaceID_2, "    ");
          _builder.append("),");
          _builder.append(calledName, "    ");
          _builder.append("), bloc_");
          _builder.append(callID, "    ");
          _builder.append(");");
          _builder.newLineIfNotEmpty();
        }
      }
    }
    CharSequence charSequenceResult = _builder;
    CompileResult _compileResult = new CompileResult(charSequenceResult, callID, 
      true);
    return _compileResult;
  }
}
